home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / system / daemon-0.000 / daemon-0 / daemon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-25  |  2.9 KB  |  155 lines

  1. #define Version 0.2
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6. #include <stdio.h>
  7.  
  8. extern int errno;
  9.  
  10. main(int ac, char *av[])
  11. {
  12. /*
  13.  * Strings we send to the client.
  14.  */
  15.  
  16.     char c;
  17.     FILE *fp;
  18.     int fromlen;
  19.     int port;
  20.     char hostname[64];
  21.     char mesg[64][1024];
  22.     char terminat[1024];
  23.     struct hostent *hp;
  24.     register int i, s, ns;
  25.     char pos;
  26.     struct sockaddr_in sin, fsin;
  27.  
  28. printf("Enter the terminating character: ");
  29. gets(terminat);
  30.  
  31.  
  32. pos=0;
  33.  
  34. printf("\n Enter the initial message: \n");
  35. do
  36. {
  37. pos++;
  38. gets(mesg[pos]);
  39. /* printf("%s\n",mesg[pos]);*/
  40. }
  41. while (mesg[pos][0]!=terminat[0]);
  42. printf("Done reading strings...\n");
  43.  
  44. printf("Converting port number...\n");
  45. port = atoi(av[1]);
  46.     /*
  47.      * Before we can do anything, we need
  48.      * to know our hostname.
  49.      */
  50.     gethostname(hostname, sizeof(hostname));
  51.  
  52.     /*
  53.      * Now we look up our host to get
  54.      * its network number.
  55.      */
  56.     if ((hp = gethostbyname(hostname)) == NULL) {
  57.         fprintf(stderr, "%s: host unknown.\n", hostname);
  58.         exit(1);
  59.     }
  60.  
  61.  
  62.     /*
  63.      * Get a socket to work with.  This socket will
  64.      * be in the Internet domain, and will be a
  65.      * stream socket.
  66.      */
  67.  
  68.  
  69. printf("Getting a socket...\n");
  70.     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  71.         perror("server: socket");
  72.         exit(1);
  73.     }
  74.  
  75.     /*
  76.      * Create the address that we will be binding to.
  77.      * We use port (cmd line) but put it into network
  78.      * byte order.  Also, we use bcopy (see 
  79.      * Chapter 14) to copy the network number.
  80.      */
  81.     sin.sin_family = AF_INET;
  82.     sin.sin_port = htons(port);
  83.     sin.sin_addr.s_addr = htonl(inet_addr("0.0.0.0"));
  84.  
  85.     /*
  86.      * Try to bind the address to the socket.
  87.      */
  88.  
  89. printf("Binding...\n");
  90.     if (bind(s, &sin, sizeof(sin)) < 0) {
  91.         perror("server: bind");
  92.         exit(1);
  93.     }
  94.  
  95.     /*
  96.      * Listen on the socket.
  97.      */
  98. printf("Listening...\n");
  99.     if (listen(s, 5) < 0) {
  100.         perror("server: listen");
  101.         exit(1);
  102.     }
  103.  
  104.     /*
  105.      * Accept connections.  When we accept one, ns
  106.      * will be connected to the client.  fsin will
  107.      * contain the address of the client.
  108.      */
  109. starting:
  110.  
  111. printf("Accepting...\n");
  112.     if ((ns = accept(s, &fsin, &fromlen)) < 0) {
  113.         perror("server: accept");
  114.         exit(1);
  115.     }
  116.  
  117.  
  118.     /*
  119.      * First we send some strings to the client.
  120.      */
  121. printf("Sending...\n");
  122.     for (i=0; i < pos+1; i++){
  123.         send(ns, mesg[i], strlen(mesg[i]), 0);
  124.         send(ns, "\n",1,0);}
  125.  
  126.     /*
  127.      * We'll use stdio for reading the socket.
  128.      */
  129. /*
  130.     fp = fdopen(ns, "r");
  131.  
  132. pos=0;
  133.  
  134. do
  135. {
  136. pos++;
  137. mesg[pos]=fgetc(fp);
  138. }
  139. while (mesg[pos]!='_');
  140.  
  141. */
  142.  
  143.  
  144.     /*
  145.      * We can simply use close() to terminate the
  146.      * connection, since we're done with both sides.
  147.      */
  148. printf("Close(ns): %d", close(ns));
  149. /* printf("Close(s): %d", close(s)); */
  150.  
  151. goto starting;
  152.     exit(0);
  153. }
  154.  
  155.